<?php
/**
 * Created by PhpStorm.
 * User: madong
 * Date: 2018/9/7
 * Time: 下午1:53
 */

namespace app\api\controller;

use think\Config;
use think\Controller;
use app\api\model\User as Usertable;
use think\Db;
use app\api\model\Adeth;
use app\api\model\Logtrue;
use app\api\model\Logomec;
use app\api\model\Logeth;
use think\Request;

class Coinall extends Controller
{
    //获取账户最新的30次交易记录
    public function Set_order_hash()
    {

        $res = Request::instance();

        $bizhong = strtolower($res->param('bizhong'));

        $hetong = Config::get('con_'.$bizhong);

        $addr = strtolower($res->param('addr'));

        if (strtolower($bizhong) == 'eth') {//如果货币类型是ETH执行

            $eth_order_hash_new_time = json_decode(file_get_contents('https://api.etherscan.io/api?module=account&action=txlist&address=' . $addr . '&startblock=0&endblock=99999999&page=1&offset=1&sort=desc&apikey=U1U6EJ1F8Z18MATPV1NG1TS52KEK717DGW'))->result[0]->timeStamp;

            $data_new_time = Db::name('hash')->where('bizhong', $bizhong)->order('timeStamp desc')->value('timeStamp');

            if ($eth_order_hash_new_time > $data_new_time) {

                $eth_order_hash = file_get_contents('https://api.etherscan.io/api?module=account&action=txlist&address=' . $addr . '&startblock=0&endblock=99999999&page=1&offset=30&sort=desc&apikey=U1U6EJ1F8Z18MATPV1NG1TS52KEK717DGW');

                $order_arr = json_decode($eth_order_hash)->result;

                $num = '';

                for ($i = 0; $i < count($order_arr); $i++) {

                    $arr_temp = array();

                    if ($order_arr[$i]->to == $addr) {

                        $eth = 1;//接收

                    } elseif ($order_arr[$i]->from == $addr) {

                        $eth = 0;//发送

                    } else {

                        $eth = 2;//合同

                    }

                    $arr_temp['blockNumber'] = $order_arr[$i]->blockNumber;//当前区块
                    $arr_temp['timeStamp'] = $order_arr[$i]->timeStamp;//成功时间
                    $arr_temp['hash'] = $order_arr[$i]->hash;//当前交易哈希
                    $arr_temp['nonce'] = $order_arr[$i]->nonce;//当前交易nonce
                    $arr_temp['blockHash'] = $order_arr[$i]->blockHash;//当前区块哈希
                    $arr_temp['transactionIndex'] = $order_arr[$i]->transactionIndex;//当前交易索引
                    $arr_temp['from'] = $order_arr[$i]->from;//当前交易发送方
                    $arr_temp['to'] = $order_arr[$i]->to;//当前交易接收方
                    $arr_temp['value'] = $order_arr[$i]->value / 1000000000000000000;//当前交易数量
                    $arr_temp['gas'] = $order_arr[$i]->gas;//当前交易gas
                    $arr_temp['gasPrice'] = $order_arr[$i]->gasPrice;//当前交易gasPrice
                    $arr_temp['isError'] = $order_arr[$i]->isError;//当前交易是否错误
                    $arr_temp['txreceipt_status'] = $order_arr[$i]->txreceipt_status;//当前交易状态
                    $arr_temp['input'] = $order_arr[$i]->input;//当前交易参数
                    $arr_temp['contractAddress'] = $order_arr[$i]->contractAddress;//合同地址
                    $arr_temp['cumulativeGasUsed'] = $order_arr[$i]->cumulativeGasUsed;//cumulativeGasUsed
                    $arr_temp['gasUsed'] = $order_arr[$i]->gasUsed / 1000000000;//gasUsed
                    $arr_temp['confirmations'] = $order_arr[$i]->confirmations;//confirmations
                    $arr_temp['bizhong'] = 'eth';//币种
                    $arr_temp['type'] = $eth;//类型

                    if (Db::name('hash')->where('hash',$order_arr[$i]->hash)->value('hash')){

                        echo '过滤掉的重复记录------'.$order_arr[$i]->hash.'<br>';

                    }else{

                        if (strlen($order_arr[$i]->input) > 4){

                            echo '这是代币转账过滤掉-------'.$order_arr[$i]->hash.'<br>';

                        }else{

                            if (Db::name('hash')->insert($arr_temp)) {

                                $num++;

                                echo '写入成功<br>';

                            } else {

                                echo '写入失败<br>';

                            }

                        }

                    }

                }

                echo '一共成功写入' . $num . '条数交易记录';

            } elseif ($eth_order_hash_new_time == $data_new_time) {

                echo '没有新订单不用更新';

            } else {

                echo 'error';

            }


        } else {//否则就是代币了

            $Rec_new_time = json_decode(file_get_contents('https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=' . $hetong . '&address=' . $addr . '&page=1&offset=1&sort=desc&apikey=U1U6EJ1F8Z18MATPV1NG1TS52KEK717DGW'))->result[0]->timeStamp;

            $Rec_data_new_time = Db::name('hash')->where('bizhong', $bizhong)->order('timeStamp desc')->value('timeStamp');

            if ($Rec_new_time > $Rec_data_new_time) {

                $data = file_get_contents('https://api.etherscan.io/api?module=account&action=tokentx&contractaddress=' . $hetong . '&address=' . $addr . '&page=1&offset=30&sort=desc&apikey=U1U6EJ1F8Z18MATPV1NG1TS52KEK717DGW');

                $order_arr = json_decode($data)->result;

                $num = '';

                for ($i = 0; $i < count($order_arr); $i++) {

                    $arr_temp = array();

                    if ($order_arr[$i]->to == $addr) {

                        $erc = 1;//接收

                    } elseif ($order_arr[$i]->from == $addr) {

                        $erc = 0;//发送

                    } else {

                        $erc = 2;//未知

                    }

                    $arr_temp["blockNumber"] = $order_arr[$i]->blockNumber;
                    $arr_temp["timeStamp"] = $order_arr[$i]->timeStamp;
                    $arr_temp["hash"] = $order_arr[$i]->hash;
                    $arr_temp["nonce"] = $order_arr[$i]->nonce;
                    $arr_temp["blockHash"] = $order_arr[$i]->blockHash;
                    $arr_temp["from"] = $order_arr[$i]->from;
                    $arr_temp["contractAddress"] = $order_arr[$i]->contractAddress;
                    $arr_temp["to"] = $order_arr[$i]->to;
                    $arr_temp["value"] = $order_arr[$i]->value / 1000000000000000000;
                    $arr_temp["tokenName"] = $order_arr[$i]->tokenName;
                    $arr_temp["tokenSymbol"] = $order_arr[$i]->tokenSymbol;
                    $arr_temp["tokenDecimal"] = $order_arr[$i]->tokenDecimal;
                    $arr_temp["transactionIndex"] = $order_arr[$i]->transactionIndex;
                    $arr_temp["gas"] = $order_arr[$i]->gas / 1000000000;
                    $arr_temp["gasPrice"] = $order_arr[$i]->gasPrice;
                    $arr_temp["gasUsed"] = $order_arr[$i]->gasUsed;
                    $arr_temp["cumulativeGasUsed"] = $order_arr[$i]->cumulativeGasUsed;
                    $arr_temp["input"] = $order_arr[$i]->input;
                    $arr_temp["confirmations"] = $order_arr[$i]->confirmations;
                    $arr_temp['bizhong'] = $bizhong;//币种
                    $arr_temp['type'] = $erc;//类型

                    if (Db::name('hash')->where('hash',$order_arr[$i]->hash)->value('hash')){

                        echo '过滤掉的重复记录------'.$order_arr[$i]->hash.'<br>';

                    }else{

                        if (Db::name('hash')->insert($arr_temp)) {

                            $num++;

                            echo '写入成功<br>';

                        } else {

                            echo '写入失败<br>';

                        }

                    }

                }

                echo '一共成功写入' . $num . '条数交易记录';

            } elseif ($Rec_new_time == $Rec_data_new_time) {

                echo '没有新订单不用更新';

            } else {

                echo 'error';

            }

        }

    }
//测试
public function ceshi(){

        $a = file_get_contents('https://www.baidu.com');

        dump($a);



}
    //输出交易记录
    public function Get_order_hash(){

        $res = Request::instance();

        $addr = strtolower($res->param('addr'));

        $bizhong = strtolower($res->param('bizhong'));

        $data = Db::name('hash')->where('from|to',$addr)->where('bizhong',$bizhong)->order('timeStamp desc')->paginate(30);

        if ($data){

            echo json_encode(['zt'=>1,'msg'=>'交易记录获取成功','data'=>$data]);

        }else{

            echo json_encode(['zt'=>0,'msg'=>'您还没有交易记录']);

        }

    }

}